package cn.yinyu.queue.module.ecg.service.devrent;

import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.security.core.util.SecurityFrameworkUtils;
import cn.yinyu.queue.module.ecg.controller.admin.jobrecord.vo.JobRecordSaveReqVO;
import cn.yinyu.queue.module.ecg.dal.dataobject.appointment.AppointmentDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.checktype.CheckTypeDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.devmanage.DeviceDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.patient.PatDetails;
import cn.yinyu.queue.module.ecg.dal.dataobject.queue.QueueDO;
import cn.yinyu.queue.module.ecg.dal.mysql.appointment.AppointmentMapper;
import cn.yinyu.queue.module.ecg.dal.mysql.checktype.CheckTypeMapper;
import cn.yinyu.queue.module.ecg.dal.mysql.devmanage.DeviceMapper;
import cn.yinyu.queue.module.ecg.dal.mysql.jobrecord.JobRecordMapper;
import cn.yinyu.queue.module.ecg.dal.mysql.queue.QueueMapper;
import cn.yinyu.queue.module.ecg.enums.DevRentStateEnum;
import cn.yinyu.queue.module.ecg.enums.DevStateEnum;
import cn.yinyu.queue.module.ecg.enums.QueueStatusEnum;
import cn.yinyu.queue.module.ecg.feign.*;
import cn.yinyu.queue.module.ecg.feign.dto.AppointmentExternal;
import cn.yinyu.queue.module.ecg.service.queue.QueueServiceTxFunctions;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.io.Console;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.yinyu.queue.module.ecg.controller.admin.devrent.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.devrent.DevRentDO;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;

import cn.yinyu.queue.module.ecg.dal.mysql.devrent.DevRentMapper;

import javax.annotation.Resource;

import static cn.yinyu.queue.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.yinyu.queue.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
import static cn.yinyu.queue.framework.common.util.date.DateUtils.getCurTimeString;
import static cn.yinyu.queue.module.ecg.enums.ErrorCodeConstants.*;

/**
 * 装机拆机 Service 实现类
 *
 * @author 垠域源码
 */
@Service
@Validated
@Slf4j
public class DevRentServiceImpl implements DevRentService {

    @Resource
    QueueServiceTxFunctions queueServiceTxFunctions;

    @Resource
    private FeeConfirmFeignService feeConfirmFeignService;

    @Resource
    private DevRentMapper devRentMapper;

    @Resource
    private JobRecordMapper jobRecordMapper;

    @Resource
    DeviceMapper deviceMapper;

    @Resource
    QueueMapper queueMapper;

    @Resource
    CheckTypeMapper checkTypeMapper;

    @Override
    public CommonResult<Long> registerOperation(DevRentSaveReqVO createReqVO) {
        Long rent_id = createDevRent(createReqVO);
        return CommonResult.success(rent_id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> routineFinishOperation(RoutineFinishReqVO routineFinishReqVO) {
        DevRentSaveReqVO createReqVO = BeanUtils.toBean(routineFinishReqVO, DevRentSaveReqVO.class);

        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();
        String userHisId = SecurityFrameworkUtils.getLoginUserHisId();

        // 在预约确认时 就进入 [dev_rent] 表了
        Long rent_id = createReqVO.getId();

        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.ROUTINE_FINISH.getState());
        jobTypeList.add( DevRentStateEnum.ROUTINE_CANCELLED.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(rent_id, userId, jobTypeList);
        if (null == jobRecordDO) {
            List<DevRentDO>  devRentDOList = devRentMapper.selectByPatAndChecktypeAndState(createReqVO.getPatId(), createReqVO.getCheckType(), DevRentStateEnum.FREE.getState());
            if (devRentDOList.size() == 0)
                throw  exception(DEV_INSTALL_NOT_APPOINTMENT);
            else if (devRentDOList.size() != 1) {
                throw  exception(DEV_INSTALL_EXCEPTION);
            }

            rent_id = devRentDOList.getFirst().getId();
            createReqVO.setId( rent_id );
            createReqVO.setState( DevRentStateEnum.ROUTINE_FINISH.getState() );
            updateDevRent( createReqVO );

            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId(rent_id);
            jobRecordSaveReqVO.setDevId(createReqVO.getDevId());
            jobRecordSaveReqVO.setDocId(userId);
            jobRecordSaveReqVO.setDocName(userNickname);
            jobRecordSaveReqVO.setPatId(createReqVO.getPatId());
            jobRecordSaveReqVO.setPatName(createReqVO.getPatName());
            jobRecordSaveReqVO.setJobTime(createReqVO.getRentTime());
            jobRecordSaveReqVO.setJobType( DevRentStateEnum.ROUTINE_FINISH.getState() );
            jobRecordSaveReqVO.setRemark(createReqVO.getRemark());
            jobRecordSaveReqVO.setSummary("");
            jobRecordSaveReqVO.setCheckType( createReqVO.getCheckType() );

            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            // 撤销 放弃操作
            createReqVO.setState( DevRentStateEnum.ROUTINE_FINISH.getState() );

            updateDevRent( createReqVO );

            jobRecordDO.setDevId(createReqVO.getDevId());
            jobRecordDO.setJobType( DevRentStateEnum.ROUTINE_FINISH.getState() );
            jobRecordDO.setJobTime(createReqVO.getRentTime());
            jobRecordDO.setRemark(createReqVO.getRemark());
            jobRecordDO.setUpdater(String.valueOf(userId));
            jobRecordDO.setUpdateTime(LocalDateTime.now());
            jobRecordMapper.updateById(jobRecordDO);
        }

        // 更新..队列状态
        //queueMapper.updateBedQueueStatus(createReqVO.getRoomId(), createReqVO.getBedNo(),
        //        QueueStatusEnum.ONSTAGE.getStatus(), QueueStatusEnum.FINISH.getStatus());

        QueueDO firstOnStageQueueItem = queueMapper.getFirstItemByBedAndStatus(createReqVO.getRoomId(),
                                            createReqVO.getBedNo(), QueueStatusEnum.ONSTAGE.getStatus());
        // QueueStatusEnum.ONSTAGE  =>  QueueStatusEnum.FINISH
        firstOnStageQueueItem.setStatus(QueueStatusEnum.FINISH.getStatus());  // 默认状态 设置
        queueMapper.updateById( firstOnStageQueueItem );

        // 检查项目.亲和性 处理逻辑
        CheckTypeDO checkTypeDO = queueServiceTxFunctions.getCheckTypeItem( firstOnStageQueueItem.getBookCheckType() );
        if (checkTypeDO.getAffinityCheckTypes().length > 0) {
            procAffinityWhenRoutineFinish( firstOnStageQueueItem.getPatId(), checkTypeDO.getAffinityCheckTypes() );
        }

        return CommonResult.success(rent_id);
    }

    /**
     * 领用完成 操作
     * @param createReqVO 创建信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> readyOperation(DevRentSaveReqVO createReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();

        // 在预约确认时 就进入 [dev_rent] 表了
        Long rent_id = createReqVO.getId();  // dev_rent 表的ID

        // 标注设备 已领取
        Integer ret = markDevRecieved(createReqVO.getDevId(), rent_id, createReqVO.getPatDetails());
        if (null == ret || 0 == ret) {
            throw exception(DEVICE_NOT_FREE);
        }

        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.READY.getState());
        jobTypeList.add( DevRentStateEnum.READY_CANCELLED.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(rent_id, userId, jobTypeList);
        if (null == jobRecordDO) {
            List<DevRentDO>  devRentDOList = devRentMapper.selectByPatAndChecktypeAndState(createReqVO.getPatId(), createReqVO.getCheckType(), DevRentStateEnum.FREE.getState());
            if (devRentDOList.size() == 0)
                throw exception(DEV_INSTALL_NOT_APPOINTMENT);
            else if (devRentDOList.size() != 1) {
                throw exception(DEV_INSTALL_EXCEPTION);
            }

            rent_id = devRentDOList.getFirst().getId();
            createReqVO.setId( rent_id );
            createReqVO.setState( DevRentStateEnum.READY.getState() );
            updateDevRent( createReqVO );

            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId(rent_id);
            jobRecordSaveReqVO.setDevId(createReqVO.getDevId());
            jobRecordSaveReqVO.setDocId(userId);
            jobRecordSaveReqVO.setDocName(userNickname);
            jobRecordSaveReqVO.setPatId(createReqVO.getPatId());
            jobRecordSaveReqVO.setPatName(createReqVO.getPatName());
            jobRecordSaveReqVO.setJobTime(createReqVO.getRentTime());
            jobRecordSaveReqVO.setJobType( DevRentStateEnum.READY.getState() );
            jobRecordSaveReqVO.setRemark(createReqVO.getRemark());
            jobRecordSaveReqVO.setSummary("");
            jobRecordSaveReqVO.setCheckType( createReqVO.getCheckType() );

            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            // 撤销 放弃操作
            createReqVO.setState( DevRentStateEnum.READY.getState() );

            updateDevRent( createReqVO );

            jobRecordDO.setDevId(createReqVO.getDevId());
            jobRecordDO.setJobType( DevRentStateEnum.READY.getState() );
            jobRecordDO.setJobTime(createReqVO.getRentTime());
            jobRecordDO.setRemark(createReqVO.getRemark());
            jobRecordDO.setUpdater(String.valueOf(userId));
            jobRecordDO.setUpdateTime(LocalDateTime.now());
            jobRecordMapper.updateById(jobRecordDO);
        }

        // 更新..队列状态
        //queueMapper.updateBedQueueStatus(createReqVO.getRoomId(), createReqVO.getBedNo(),
        //        QueueStatusEnum.ONSTAGE.getStatus(), QueueStatusEnum.RECEIVED.getStatus());

        // QueueStatusEnum.ONSTAGE  =>  QueueStatusEnum.RECEIVED
        QueueDO bedOnStageQueueItem = queueMapper.getFirstItemByBedAndStatus(
                createReqVO.getRoomId(), createReqVO.getBedNo(), QueueStatusEnum.ONSTAGE.getStatus() );
        CheckTypeDO checkTypeDO = queueServiceTxFunctions.getCheckTypeItem( bedOnStageQueueItem.getBookCheckType() );
        bedOnStageQueueItem.setStatus(QueueStatusEnum.RECEIVED.getStatus());  // [已领用] 状态

        // 检查项目.亲和性 处理逻辑 , 更新 bedOnStageQueueItem
        if (checkTypeDO.getAffinityCheckTypes().length > 0) {
            procAffinityWhenReadyFinish(/*IN, OUT*/bedOnStageQueueItem, checkTypeDO.getAffinityCheckTypes());
        }

        queueMapper.updateById( bedOnStageQueueItem );
        return CommonResult.success(rent_id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> installOperation(DevRentSaveReqVO createReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();
        Long rent_id = createReqVO.getId();

        DevRentDO devRentDO = getDevRent(rent_id);

        // 对于已有领用，但是装机时使用的不是已领用设备，报错
        if ( null != devRentDO.getDevId() && !createReqVO.getDevId().equals(devRentDO.getDevId()) ) {
            throw exception(DEVICE_NOT_PAT_RECEIVED);
        }

        // 设置设备 使用中
        Integer ret = markDevInUse(createReqVO.getDevId(), rent_id);
        if (null == ret || 0 == ret) {
            throw exception(DEVICE_NOT_RECEIVED);
        }

        // 检查 job_record 用于确认是否是第一次安装操作
        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.INSTALLED.getState());
        jobTypeList.add( DevRentStateEnum.INSTALL_CANCELLED.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(rent_id, userId, jobTypeList);

        // 第一次 安装操作
        if (null == jobRecordDO) {
            List<DevRentDO>  devRentDOList = devRentMapper.selectByPatAndChecktypeAndState(createReqVO.getPatId(), createReqVO.getCheckType(), DevRentStateEnum.READY.getState());
            if (devRentDOList.size() == 0)
                throw exception(DEV_INSTALL_NOT_RECEIVED);
            else if (devRentDOList.size() != 1) {
                throw exception(DEV_INSTALL_EXCEPTION);
            }

            rent_id = devRentDOList.getFirst().getId();
            createReqVO.setId( rent_id );
            createReqVO.setState( DevRentStateEnum.INSTALLED.getState() );
            updateDevRent( createReqVO );

            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId(rent_id);
            jobRecordSaveReqVO.setDevId(createReqVO.getDevId());
            jobRecordSaveReqVO.setDocId(userId);
            jobRecordSaveReqVO.setDocName(userNickname);
            jobRecordSaveReqVO.setPatId(createReqVO.getPatId());
            jobRecordSaveReqVO.setPatName(createReqVO.getPatName());
            jobRecordSaveReqVO.setJobTime(createReqVO.getRentTime());
            jobRecordSaveReqVO.setJobType( DevRentStateEnum.INSTALLED.getState() );
            jobRecordSaveReqVO.setRemark(createReqVO.getRemark());
            jobRecordSaveReqVO.setSummary("");
            jobRecordSaveReqVO.setCheckType( createReqVO.getCheckType() );

            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            // 撤销 放弃操作
            createReqVO.setState( DevRentStateEnum.INSTALLED.getState() );
            updateDevRent( createReqVO );

            jobRecordDO.setDevId(createReqVO.getDevId());
            jobRecordDO.setJobType( DevRentStateEnum.INSTALLED.getState() );
            jobRecordDO.setJobTime(createReqVO.getRentTime());
            jobRecordDO.setRemark(createReqVO.getRemark());
            jobRecordDO.setUpdater(String.valueOf(userId));
            jobRecordDO.setUpdateTime(LocalDateTime.now());
            jobRecordMapper.updateById(jobRecordDO);
        }

        // 更新..队列状态
        //queueMapper.updateBedQueueStatus(createReqVO.getRoomId(), createReqVO.getBedNo(),
        //        QueueStatusEnum.INSTALLING.getStatus(), QueueStatusEnum.FINISH.getStatus());

        // QueueStatusEnum.INSTALLING  =>  QueueStatusEnum.FINISH
        QueueDO bedInstallingQueueItem = queueMapper.getFirstItemByBedAndStatus(
                            createReqVO.getRoomId(), createReqVO.getBedNo(), QueueStatusEnum.INSTALLING.getStatus());
        bedInstallingQueueItem.setStatus(QueueStatusEnum.FINISH.getStatus());  // 默认状态 设置
        queueMapper.updateById( bedInstallingQueueItem );

        // 检查项目.亲和性 处理逻辑
        CheckTypeDO checkTypeDO = queueServiceTxFunctions.getCheckTypeItem( bedInstallingQueueItem.getBookCheckType() );
        if (checkTypeDO.getAffinityCheckTypes().length > 0) {
            procAffinityWhenInstallFinish(bedInstallingQueueItem, checkTypeDO.getAffinityCheckTypes());
        }

        return CommonResult.success(rent_id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long dismantleOperation(DevRentSaveReqVO updateReqVO) {
        updateReqVO.setState( DevRentStateEnum.DISMANTLED.getState() );
        updateDevRent( updateReqVO );
        Long rentId = updateReqVO.getId();

        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();

        // 设备恢复到空闲
        markDevFree( updateReqVO.getDevId(), rentId );

        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.DISMANTLED.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(updateReqVO.getId(), userId, jobTypeList);
        if (null == jobRecordDO) {
            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId( updateReqVO.getId() );
            jobRecordSaveReqVO.setDevId( updateReqVO.getDevId() );
            jobRecordSaveReqVO.setDocId( userId );
            jobRecordSaveReqVO.setDocName( userNickname );
            jobRecordSaveReqVO.setPatId( updateReqVO.getPatId() );
            jobRecordSaveReqVO.setPatName( updateReqVO.getPatName() );
            jobRecordSaveReqVO.setJobTime( updateReqVO.getReturnTime() );
            jobRecordSaveReqVO.setJobType( DevRentStateEnum.DISMANTLED.getState() );
            jobRecordSaveReqVO.setRemark( updateReqVO.getRemark() );
            jobRecordSaveReqVO.setSummary( updateReqVO.getInterference() + " " + updateReqVO.getBaseline() + " " + updateReqVO.getDetachment() );
            jobRecordSaveReqVO.setCheckType( updateReqVO.getCheckType() );
            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            jobRecordDO.setJobType( DevRentStateEnum.DISMANTLED.getState() );
            jobRecordDO.setJobTime( updateReqVO.getReturnTime() );
            jobRecordDO.setRemark( updateReqVO.getRemark() );
            jobRecordDO.setSummary( updateReqVO.getInterference() + " " + updateReqVO.getBaseline() + " " + updateReqVO.getDetachment() );
            jobRecordDO.setUpdater( String.valueOf(userId) );
            jobRecordDO.setUpdateTime( LocalDateTime.now() );
            jobRecordMapper.updateById(jobRecordDO);
        }
        return updateReqVO.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long dataEntryOperation(DevRentSaveReqVO updateReqVO) {
        updateReqVO.setState( DevRentStateEnum.DATAENTERED.getState() );
        updateDevRent( updateReqVO );

        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();

        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.DATAENTERED.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(updateReqVO.getId(), userId, jobTypeList);
        if (null == jobRecordDO) {
            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId( updateReqVO.getId() );
            jobRecordSaveReqVO.setDevId( updateReqVO.getDevId() );
            jobRecordSaveReqVO.setDocId( userId );
            jobRecordSaveReqVO.setDocName( userNickname );
            jobRecordSaveReqVO.setPatId( updateReqVO.getPatId() );
            jobRecordSaveReqVO.setPatName( updateReqVO.getPatName() );
            jobRecordSaveReqVO.setJobTime( updateReqVO.getEntryTime() );
            jobRecordSaveReqVO.setJobType( DevRentStateEnum.DATAENTERED.getState() );
            jobRecordSaveReqVO.setRemark( updateReqVO.getRemark() );
            jobRecordSaveReqVO.setSummary( updateReqVO.getInterference() + " " + updateReqVO.getBaseline() + " " + updateReqVO.getDetachment() );
            jobRecordSaveReqVO.setCheckType( updateReqVO.getCheckType() );
            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            jobRecordDO.setJobType( DevRentStateEnum.DATAENTERED.getState() );
            jobRecordDO.setJobTime( updateReqVO.getEntryTime() );
            jobRecordDO.setRemark( updateReqVO.getRemark() );
            jobRecordDO.setSummary( updateReqVO.getInterference() + " " + updateReqVO.getBaseline() + " " + updateReqVO.getDetachment() );
            jobRecordDO.setUpdater( String.valueOf(userId) );
            jobRecordMapper.updateById(jobRecordDO);
        }
        return updateReqVO.getId();
    }

    // 常规检查 取消
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> cancelRoutineOperation(DevCancelReqVO cancelReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();

        DevRentSaveReqVO createReqVO = BeanUtils.toBean(cancelReqVO, DevRentSaveReqVO.class);
        createReqVO.setDevId(null);
        createReqVO.setState( DevRentStateEnum.ROUTINE_CANCELLED.getState());

        Long rent_id = createReqVO.getId();

        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.ROUTINE_FINISH.getState());
        jobTypeList.add( DevRentStateEnum.ROUTINE_CANCELLED.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(rent_id, userId, jobTypeList);
        if (null == jobRecordDO) {
            List<DevRentDO>  devRentDOList = devRentMapper.selectByPatAndChecktypeAndState(createReqVO.getPatId(), createReqVO.getCheckType(), DevRentStateEnum.FREE.getState());
            if (devRentDOList.size() != 1)
                throw exception(ROUTINE_CHECK_EXIST);

            rent_id = devRentDOList.getFirst().getId();
            createReqVO.setId( rent_id );
            createReqVO.setState( DevRentStateEnum.ROUTINE_CANCELLED.getState() );
            updateDevRent( createReqVO );

            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId(rent_id);
            jobRecordSaveReqVO.setDevId(null);
            jobRecordSaveReqVO.setDocId(userId);
            jobRecordSaveReqVO.setDocName(userNickname);
            jobRecordSaveReqVO.setPatId(createReqVO.getPatId());
            jobRecordSaveReqVO.setPatName(createReqVO.getPatName());
            jobRecordSaveReqVO.setJobType( DevRentStateEnum.ROUTINE_CANCELLED.getState() );
            jobRecordSaveReqVO.setJobTime(createReqVO.getRentTime());
            jobRecordSaveReqVO.setRemark(createReqVO.getRemark());
            jobRecordSaveReqVO.setSummary("");
            jobRecordSaveReqVO.setCheckType( createReqVO.getCheckType() );

            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            updateDevRent( createReqVO );

            jobRecordDO.setDevId(null);
            jobRecordDO.setJobType(DevRentStateEnum.ROUTINE_CANCELLED.getState());
            jobRecordDO.setJobTime(createReqVO.getRentTime());
            jobRecordDO.setRemark(createReqVO.getRemark());
            jobRecordDO.setUpdater(String.valueOf(userId));
            jobRecordDO.setUpdateTime(LocalDateTime.now());
            jobRecordMapper.updateById(jobRecordDO);
        }
        return CommonResult.success(rent_id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> cancelReadyOperation(DevCancelReqVO cancelReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();
        Long rentId = cancelReqVO.getId();

        // 若从 装机 改为 取消装机，恢复设备状态
        if (null != cancelReqVO.getDevId()) {
            markDevFree( cancelReqVO.getDevId(),  rentId );
        }

        DevRentSaveReqVO createReqVO = BeanUtils.toBean(cancelReqVO, DevRentSaveReqVO.class);
        createReqVO.setDevId(null);
        createReqVO.setState( DevRentStateEnum.READY_CANCELLED.getState());

        Long rent_id = createReqVO.getId();

        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.READY.getState());
        jobTypeList.add( DevRentStateEnum.READY_CANCELLED.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(rent_id, userId, jobTypeList);
        if (null == jobRecordDO) {
            List<DevRentDO>  devRentDOList = devRentMapper.selectByPatAndChecktypeAndState(createReqVO.getPatId(), createReqVO.getCheckType(), DevRentStateEnum.FREE.getState());
            if (devRentDOList.size() != 1)
                throw exception(DEV_INSTALL_EXIST);

            rent_id = devRentDOList.getFirst().getId();
            createReqVO.setId( rent_id );
            createReqVO.setState( DevRentStateEnum.READY_CANCELLED.getState() );
            updateDevRent( createReqVO );

            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId(rent_id);
            jobRecordSaveReqVO.setDevId(null);
            jobRecordSaveReqVO.setDocId(userId);
            jobRecordSaveReqVO.setDocName(userNickname);
            jobRecordSaveReqVO.setPatId(createReqVO.getPatId());
            jobRecordSaveReqVO.setPatName(createReqVO.getPatName());
            jobRecordSaveReqVO.setJobType( DevRentStateEnum.READY_CANCELLED.getState() );
            jobRecordSaveReqVO.setJobTime(createReqVO.getRentTime());
            jobRecordSaveReqVO.setRemark(createReqVO.getRemark());
            jobRecordSaveReqVO.setSummary("");
            jobRecordSaveReqVO.setCheckType( createReqVO.getCheckType() );

            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            updateDevRent( createReqVO );

            jobRecordDO.setDevId(null);
            jobRecordDO.setJobType(DevRentStateEnum.READY_CANCELLED.getState());
            jobRecordDO.setJobTime(createReqVO.getRentTime());
            jobRecordDO.setRemark(createReqVO.getRemark());
            jobRecordDO.setUpdater(String.valueOf(userId));
            jobRecordDO.setUpdateTime(LocalDateTime.now());
            jobRecordMapper.updateById(jobRecordDO);
        }
        return CommonResult.success(rent_id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> cancelInstallOperation(DevCancelReqVO cancelReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();
        Long rentId = cancelReqVO.getId();

        // 若从 装机 改为 取消装机，恢复设备状态
        if (null != cancelReqVO.getDevId()) {
            markDevFree( cancelReqVO.getDevId(), rentId );
        }

        DevRentSaveReqVO createReqVO = BeanUtils.toBean(cancelReqVO, DevRentSaveReqVO.class);
        createReqVO.setDevId(null);
        createReqVO.setState( DevRentStateEnum.INSTALL_CANCELLED.getState() );

        Long rent_id = createReqVO.getId();

        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.INSTALLED.getState());
        jobTypeList.add( DevRentStateEnum.INSTALL_CANCELLED.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(rent_id, userId, jobTypeList);
        if (null == jobRecordDO) {
            List<DevRentDO>  devRentDOList = devRentMapper.selectByPatAndChecktypeAndState(createReqVO.getPatId(), createReqVO.getCheckType(), DevRentStateEnum.READY.getState());
            if (devRentDOList.size() != 1)
                throw exception(DEV_INSTALL_EXIST);

            rent_id = devRentDOList.getFirst().getId();
            createReqVO.setId( rent_id );
            createReqVO.setState( DevRentStateEnum.INSTALL_CANCELLED.getState() );
            updateDevRent( createReqVO );

            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId(rent_id);
            jobRecordSaveReqVO.setDevId(null);
            jobRecordSaveReqVO.setDocId(userId);
            jobRecordSaveReqVO.setDocName(userNickname);
            jobRecordSaveReqVO.setPatId(createReqVO.getPatId());
            jobRecordSaveReqVO.setPatName(createReqVO.getPatName());
            jobRecordSaveReqVO.setJobType( DevRentStateEnum.INSTALL_CANCELLED.getState() );
            jobRecordSaveReqVO.setJobTime(createReqVO.getRentTime());
            jobRecordSaveReqVO.setRemark(createReqVO.getRemark());
            jobRecordSaveReqVO.setSummary("");
            jobRecordSaveReqVO.setCheckType( createReqVO.getCheckType() );

            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            updateDevRent( createReqVO );

            jobRecordDO.setDevId(null);
            jobRecordDO.setJobType( DevRentStateEnum.INSTALL_CANCELLED.getState() );
            jobRecordDO.setJobTime(createReqVO.getRentTime());
            jobRecordDO.setRemark(createReqVO.getRemark());
            jobRecordDO.setUpdater(String.valueOf(userId));
            jobRecordDO.setUpdateTime(LocalDateTime.now());
            jobRecordMapper.updateById(jobRecordDO);
        }
        return CommonResult.success(rent_id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> dismantleExceptionOperation(DevRentSaveReqVO updateReqVO) {
        DevStateEnum devStateEnum = null;
        DevRentStateEnum devRentStateEnum = null;
        if (updateReqVO.getState() == DevStateEnum.DAMAGED.getState()) {
            devStateEnum = DevStateEnum.DAMAGED;
            devRentStateEnum = DevRentStateEnum.DAMAGED;
        }
        else if (updateReqVO.getState() == DevStateEnum.LOST.getState()) {
            devStateEnum = DevStateEnum.LOST;
            devRentStateEnum = DevRentStateEnum.LOST;
        }
        else
            throw exception(DEV_DISMANTLE_EXCEPTION);

        // 设备遗失时, 标注遗失
        markDevDismantleException( updateReqVO.getDevId(), updateReqVO.getId(), updateReqVO.getReturnTime().toLocalDate(), devStateEnum );

        updateReqVO.setState( devRentStateEnum.getState() );
        updateDevRent( updateReqVO );

        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String userNickname = SecurityFrameworkUtils.getLoginUserNickname();

        List<Integer> jobTypeList = new ArrayList<Integer>();
        jobTypeList.add( DevRentStateEnum.DAMAGED.getState());
        jobTypeList.add( DevRentStateEnum.LOST.getState());
        JobRecordDO jobRecordDO = jobRecordMapper.selectByRentIdDocId(updateReqVO.getId(), userId, jobTypeList);
        if (null == jobRecordDO) {
            JobRecordSaveReqVO jobRecordSaveReqVO = new JobRecordSaveReqVO();
            jobRecordSaveReqVO.setRentId( updateReqVO.getId() );
            jobRecordSaveReqVO.setDevId( updateReqVO.getDevId() );
            jobRecordSaveReqVO.setDocId( userId );
            jobRecordSaveReqVO.setDocName( userNickname );
            jobRecordSaveReqVO.setPatId( updateReqVO.getPatId() );
            jobRecordSaveReqVO.setPatName( updateReqVO.getPatName() );
            jobRecordSaveReqVO.setJobTime( updateReqVO.getReturnTime() );
            jobRecordSaveReqVO.setJobType( devRentStateEnum.getState() );
            jobRecordSaveReqVO.setRemark( updateReqVO.getRemark() );
            jobRecordSaveReqVO.setSummary( updateReqVO.getInterference() + " " + updateReqVO.getBaseline() + " " + updateReqVO.getDetachment() );
            jobRecordSaveReqVO.setCheckType( updateReqVO.getCheckType() );
            JobRecordDO jobRecord = BeanUtils.toBean(jobRecordSaveReqVO, JobRecordDO.class);
            jobRecordMapper.insert(jobRecord);
        } else {
            jobRecordDO.setJobType( devRentStateEnum.getState() );
            jobRecordDO.setJobTime( updateReqVO.getReturnTime() );
            jobRecordDO.setRemark( updateReqVO.getRemark() );
            jobRecordDO.setSummary( updateReqVO.getInterference() + " " + updateReqVO.getBaseline() + " " + updateReqVO.getDetachment() );
            jobRecordDO.setUpdater( String.valueOf(userId) );
            jobRecordDO.setUpdateTime( LocalDateTime.now() );
            jobRecordMapper.updateById(jobRecordDO);
        }

        return CommonResult.success(updateReqVO.getId());
    }

    @Override
    public Long createDevRent(DevRentSaveReqVO createReqVO) {
        // 插入
        DevRentDO devRent = BeanUtils.toBean(createReqVO, DevRentDO.class);
        devRentMapper.insert(devRent);
        // 返回
        return devRent.getId();
    }

    @Override
    public void updateDevRent(DevRentSaveReqVO updateReqVO) {
        // 校验存在
        validateDevRentExists(updateReqVO.getId());
        // 更新
        DevRentDO updateObj = BeanUtils.toBean(updateReqVO, DevRentDO.class);
        devRentMapper.updateById(updateObj);
    }

    @Override
    public void deleteDevRent(Long id) {
        // 校验存在
        validateDevRentExists(id);
        // 删除
        devRentMapper.deleteById(id);
    }

    private void validateDevRentExists(Long id) {
        if (devRentMapper.selectById(id) == null) {
            throw exception(DEV_INSTALL_NOT_EXISTS);
        }
    }

    @Override
    public DevRentDO getDevRent(Long id) {
        return devRentMapper.selectById(id);
    }

    @Override
    public PageResult<DevRentDO> getDevRentPage(DevRentPageReqVO pageReqVO) {
        return devRentMapper.selectPage(pageReqVO);
    }

    @Override
    public DevRentDO getInstalledOrCancelledRent(DevRentSearchReqVO searchReqVO) {
        List<Integer> stateList = new ArrayList<>();
        stateList.add( DevRentStateEnum.INSTALLED.getState() );
        stateList.add( DevRentStateEnum.INSTALL_CANCELLED.getState());
        searchReqVO.setStateList( stateList );
        return devRentMapper.getRentByState(searchReqVO);
    }

    @Override
    public DevRentDO getFreeRent(DevRentSearchReqVO searchReqVO) {
        List<Integer> stateList = new ArrayList<>();
        stateList.add( DevRentStateEnum.FREE.getState() );
        searchReqVO.setStateList( stateList );
        return devRentMapper.getRentByState(searchReqVO);
    }

    @Override
    public DevRentDO getReadyOrCancelledRent(DevRentSearchReqVO searchReqVO) {
        List<Integer> stateList = new ArrayList<>();
        stateList.add( DevRentStateEnum.READY.getState() );
        stateList.add( DevRentStateEnum.READY_CANCELLED.getState());
        searchReqVO.setStateList( stateList );
        return devRentMapper.getRentByState(searchReqVO);
    }

    @Override
    public DevRentDO getDismantledRent(DevRentSearchReqVO searchReqVO) {
        List<Integer> stateList = new ArrayList<>();
        stateList.add( DevRentStateEnum.DISMANTLED.getState() );
        searchReqVO.setStateList( stateList );
        return devRentMapper.getRentByState(searchReqVO);
    }

    // 确费处理
    // 返回值:  0 成功  1 失败  2 不用确费
    @Override
    public Integer feeConfirm(Long rentId, String hisId, String userNickname, Boolean isFeeConfirmOrCancel) {
        DevRentDO devRentDO = devRentMapper.selectById(rentId);

        // 查询检查类型，确定是否需要确费
        CheckTypeDO checkTypeDO = checkTypeMapper.getCheckTypeByValue(devRentDO.getCheckType());
        if (0 == checkTypeDO.getExpenseRecognition()) {
            return 2;
        }

        PatDetails patDetails = devRentDO.getPatDetails();
        HisFeeConfirmReqBody hisFeeConfirmReqBody = new HisFeeConfirmReqBody();
        MsgHeader msgHeader = new MsgHeader();
        msgHeader.setMsgType("ODS_2212");
        msgHeader.setMsgVersion("3.0");
        msgHeader.setSender("ECG");
        hisFeeConfirmReqBody.setMsgHeader(msgHeader);
        Item item = new Item();
        item.setItemCode( getHisCheckCode(devRentDO.getCheckType()) ); //
        item.setItemStatus( isFeeConfirmOrCancel? "5" : "6" ); // 5 确费 6 取消确费
        item.setExeOrganization("47162057-2");
        item.setExeDeptName("心电科");
        item.setExeDept("心电科");
        item.setExeDoctor( hisId ); //
        item.setExeDoctorName( userNickname ); //
        item.setExeDateTime( getCurTimeString() ); //
        ExmRequest exmRequest = new ExmRequest();
        exmRequest.setAuthorOrganization("47162057-2");
        exmRequest.setRequestId( devRentDO.getApplyNo() );  //
        exmRequest.setPatientType( getPatientType(patDetails.getSource()) ); //
        exmRequest.setItem(item);
        hisFeeConfirmReqBody.setExmRequest(exmRequest);
        HisFeeConfirmRespResult result = feeConfirmFeignService.httpFeeApi("UpdateExmRequestStatus", "ECG", "ECG", hisFeeConfirmReqBody);
        log.info( result.getMsgHeader().getStatus() );
        Integer returnValue = result.getMsgHeader().getStatus().equals("true") ? 0 : 1;
        if (0 == returnValue) {
            devRentMapper.setPaid(rentId, isFeeConfirmOrCancel ? 1 : 0);
        }
        return returnValue;  // 0 成功  1 失败  2 不用确费
    }

    // 标记 设备已被领取
    private Integer markDevRecieved(String devId, Long rentId, PatDetails patDetails) {
        // 设备标注 已领取
        DeviceDO deviceDO = deviceMapper.getDeviceByDevId(devId);
        if (null != deviceDO && DevStateEnum.FREE.isEqual(deviceDO.getState())) {
            deviceDO.setState( DevStateEnum.RECEIVED.getState() );
            deviceDO.setRentId( rentId );
            deviceDO.setStateDate( LocalDate.now() );
            deviceDO.setPatDetails( patDetails );
            return deviceMapper.updateById(deviceDO);
        }

        return 0;
    }

    // 标记 设备已被使用
    private Integer markDevInUse(String devId, Long rentId) {
        DeviceDO deviceDO = deviceMapper.getDeviceByDevId(devId);
        if (null == deviceDO)
            return 0;

        if (DevStateEnum.RECEIVED.isEqual(deviceDO.getState()) && rentId.equals(deviceDO.getRentId())) {
            deviceDO.setState( DevStateEnum.INUSE.getState() );
            deviceDO.setStateDate( LocalDate.now() );
            return deviceMapper.updateById(deviceDO);
        }

        if (DevStateEnum.FREE.isEqual(deviceDO.getState())) {
            deviceDO.setState( DevStateEnum.INUSE.getState() );
            deviceDO.setStateDate( LocalDate.now() );
            deviceDO.setRentId( rentId );
            return deviceMapper.updateById(deviceDO);
        }

        return 0;
    }

    private Integer markDevDismantleException(String devId, Long rentId, LocalDate localDate, DevStateEnum devStateEnum) {
        // 设备标注遗失
        DeviceDO deviceDO = deviceMapper.getDeviceByDevId(devId);
        if (null != deviceDO && DevStateEnum.INUSE.isEqual(deviceDO.getState()) && rentId.equals(deviceDO.getRentId())) {
            deviceDO.setState( devStateEnum.getState() );
            deviceDO.setStateDate( localDate );
            return deviceMapper.updateById(deviceDO);
        }

        return 0;
    }

    private Integer markDevFree(String devId, Long rentId) {
        // 设备标注遗失 恢复为空闲
        DeviceDO deviceDO = deviceMapper.getDeviceByDevId( devId );
        if (null != deviceDO && rentId.equals(deviceDO.getRentId())) {
            deviceDO.setState( DevStateEnum.FREE.getState() );
            deviceDO.setStateDate( LocalDate.now() );
            deviceDO.setRentId(null);
            deviceDO.setPatDetails(null);
            return deviceMapper.updateById(deviceDO);
        }
        
        return 0;
    }

    private void procAffinityWhenRoutineFinish(String patId, Integer[] affinityCheckTypes) {
        // 如果存在 [亲和-排队] 亲和项，则选择 其中一个亲和项 接替其 处于 [就诊中]
        List<QueueDO> affinityAffinityWaitingItems = queueMapper.getCurPatGivenCheckTypesAndStatus(
                                patId, affinityCheckTypes, QueueStatusEnum.AFFINITY_WAITING.getStatus());
        if (!affinityAffinityWaitingItems.isEmpty()) {
            QueueDO tmpQueueDO = affinityAffinityWaitingItems.get(0);
            tmpQueueDO.setStatus(QueueStatusEnum.ONSTAGE.getStatus());
            queueMapper.updateById(tmpQueueDO);
        }
    }

    private void procAffinityWhenReadyFinish(/*IN,OUT*/QueueDO bedOnStageQueueItem, Integer[] affinityCheckTypes) {
        // 如果存在 [安装中] 亲和项，则跟随其中一个亲和项
        List<QueueDO> affinityInstallingItems = queueMapper.getCurPatGivenCheckTypesAndStatus(
                bedOnStageQueueItem.getPatId(), affinityCheckTypes, QueueStatusEnum.INSTALLING.getStatus());
        if (!affinityInstallingItems.isEmpty()) {
            QueueDO affinityInstallingItem = affinityInstallingItems.get(0);
            bedOnStageQueueItem.setStatus(QueueStatusEnum.AFFINITY_RECEIVED.getStatus()); // 亲和-领用
            bedOnStageQueueItem.setRoomId( affinityInstallingItem.getRoomId() );
            bedOnStageQueueItem.setBedNo( affinityInstallingItem.getBedNo() );  // 领用 到 安装 可能会在不同工位
        }

        // 如果存在 [亲和-排队] 亲和项，则选择 其中一个亲和项 接替其 处于 [就诊中]
        List<QueueDO> affinityAffinityWaitingItems = queueMapper.getCurPatGivenCheckTypesAndStatus(
                bedOnStageQueueItem.getPatId(), affinityCheckTypes, QueueStatusEnum.AFFINITY_WAITING.getStatus());
        if (!affinityAffinityWaitingItems.isEmpty()) {
            QueueDO tmpQueueDO = affinityAffinityWaitingItems.get(0);
            tmpQueueDO.setStatus(QueueStatusEnum.ONSTAGE.getStatus());
            queueMapper.updateById( tmpQueueDO );
        }
    }

    private void procAffinityWhenInstallFinish(QueueDO bedInstallingQueueItem, Integer[] affinityCheckTypes) {
        List<QueueDO> affinityAffinityReceivedItems = queueMapper.getCurPatGivenCheckTypesAndStatus(
                bedInstallingQueueItem.getPatId(), affinityCheckTypes, QueueStatusEnum.AFFINITY_RECEIVED.getStatus());
        if (!affinityAffinityReceivedItems.isEmpty()) {
            QueueDO tmpQueueDO = affinityAffinityReceivedItems.get(0);
            tmpQueueDO.setStatus(QueueStatusEnum.INSTALLING.getStatus());
            tmpQueueDO.setRoomId( bedInstallingQueueItem.getRoomId() );
            tmpQueueDO.setBedNo( bedInstallingQueueItem.getBedNo() ); // 从 [领用] 到 [安装]， 可鞥在不同工位上操作
            queueMapper.updateById(tmpQueueDO);
        }
    }

    private String getHisCheckCode(Integer checkType) {
        if (100 == checkType)
            return "691133607";
        else if (200 == checkType)
            return "201605";
        else if (300 == checkType)
            return "200327";
        else if (400 == checkType)
            return "201652";
        else if (500 == checkType)
            return "502490914";
        else if (600 == checkType)
            return "419562119";
        else if (700 == checkType)
            return "201604";
        else if (800 == checkType)
            return "1202042";
        else if (900 == checkType)
            return "1202058";
        else if (1000 == checkType)
            return "1202065";
        else if (1100 == checkType)
            return "559542128";
        else if (1200 == checkType)
            return "590244511";
        else if (1300 == checkType)
            return "666454217";
        else if (1400 == checkType)
            return "720791490";
        else if (1500 == checkType)
            return "720792077";

        return "691133607";
    }

    String getPatientType( Integer patientSource ) {
        if (1 == patientSource || 2 == patientSource)
            return "01";
        else if (3 == patientSource)
            return "03";
        else if (4 == patientSource)
            return "04";

        return "01";
    }

}

